﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;
using Telerik.Web.UI;
using KPIS.GERP.ESS.BLL;
using KPIS.GERP.ESS.MODEL;

namespace KPIS.GERP.ESS.APP.App_UI.Forms
{
    public partial class RequestTrainings : System.Web.UI.Page
    {
        private CommandControlBLL cmdFac = new CommandControlBLL();
        private HrmTrainingBLL trBLL = new HrmTrainingBLL();
        private B_HrmPositions posBLL = new B_HrmPositions();
        private B_MessageSend sendBLL = new B_MessageSend();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadDataGrid();
                LoadTrainingCourses();
                LoadApprover();
            }
        }

        private void LoadDataGrid()
        {
            DataSet ListDataCommand = trBLL.LoadTrainingTrainee(null, Int32.Parse(Session["EmpSeq"].ToString()), null);
            DataTable table = ListDataCommand.Tables[0];

            RadGrid2.DataSource = ListDataCommand;
            RadGrid2.DataBind();
        }
        protected void RadGrid2_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {

            }
            if (e.CommandName == "AddData")
            {
                //UpdateUI2("AddNew");
            }
            LoadDataGrid();
        }
        protected void LoadTrainingCourses()
        {
            DataSet ds = trBLL.LoadTrainingCourses(null, false);

            RadGridCourses.DataSource = ds;
            RadGridCourses.DataBind();
        }
        protected void RadGridCourses_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {

            }
            if (e.CommandName == "AddData")
            {
                //UpdateUI2("AddNew");
            }
            LoadDataGrid();
        }
        private void LoadApprover()
        {
            DataSet ds = posBLL.GetPositionUpper(Int32.Parse(Session["EmpSeq"].ToString()), (int?)null, "");

            cmbAppSeq.DataSource = ds;
            cmbAppSeq.DataBind();
        }
        public void GetKeyValue(string Sequence)
        {
            DataSet ds = trBLL.LoadTrainingCourses(Int32.Parse(Sequence), false);
            DataTable dt = null;
            if (ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];

                lblTrainSequence.Text = dt.Rows[0]["train_seq"].ToString();
                lblCourseDescr.Text = dt.Rows[0]["training_course_descr"].ToString();
                lbDateTrainStard.Text = dt.Rows[0]["date_training_start"].ToString();
                lbDateTrainEnd.Text = dt.Rows[0]["date_training_end"].ToString();
                lbDateSignStard.Text = dt.Rows[0]["date_sign_start"].ToString();
                lbDateSignEnd.Text = dt.Rows[0]["date_sign_end"].ToString();
                lblSchedule.Text = dt.Rows[0]["schedule_text"].ToString();
                lblOrg.Text = dt.Rows[0]["org_text"].ToString();
            }

            cmdFac.ViewCommand(panelList, PanelCourses, panelCommand);
            lblTrainSequence.Visible = false;
        }
        protected void RadManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
        {
            if (e.Argument == "CloseWindow")
            {
            }
            else
            {
                GetKeyValue(e.Argument);
                cmdShowCourses.Visible = false;
                btnSave.Visible = true;
                btnCancel.Visible = true;
            }
        }
        protected void cmdShowAll_Click(object sender, EventArgs e)
        {
            btnSave.Visible = false;
            btnCancel.Visible = false;

            PanelCourses.Visible = false;
            PanelGrid2.Visible = true;
        }
        protected void cmdShowCourses_Click(object sender, EventArgs e)
        {           

            PanelCourses.Visible = true;
            PanelGrid2.Visible = false;
        }
        protected void SendMessage()
        {
            M_MessageSend info = new M_MessageSend();

            info.from_emp_seq = Int32.Parse(Session["EmpSeq"].ToString());
            info.from_pos_seq = sendBLL.GetPosSeqFromEmpSeq(Session["EmpSeq"].ToString());
            info.to_emp_seq = Int32.Parse(cmbAppSeq.SelectedValue);
            info.to_pos_seq = sendBLL.GetPosSeqFromEmpSeq(cmbAppSeq.SelectedValue);
            info.ref_seq = Int32.Parse(lblTrainSequence.Text.Trim());
            info.ref_no = sendBLL.GetNextSendRefNo("TR");
            info.ref_type = "hrm_training_requests";
            info.send_date = DateTime.Today;
            info.send_text = "ขออนุมัติเข้ารับการอบรมหลักสูตร" + lblCourseDescr.Text.Trim();
            info.message_status = "0";
            info.created_by = Int32.Parse(Session["EmpSeq"].ToString());
            info.created_when = DateTime.Now;

            sendBLL.Insert(info);
        }
        protected void btnEdit_Click(object sender, EventArgs e)
        {

        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            cmdFac.GridCommand(panelList, PanelCourses, panelCommand);

            cmdShowCourses.Visible = true;
            btnSave.Visible = false;
            btnCancel.Visible = false;
        }
        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            SendMessage();
            cmdFac.GridCommand(panelList, PanelCourses, panelCommand);

            cmdShowCourses.Visible = true;
            btnSave.Visible = false;
            btnCancel.Visible = false;
        }
    }
}
